// page/category/prolist.js
var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置
Page({
  data: {
      isHide: 0, 
      tabs: ["", "", ""],
      activeIndex:0,
      sliderOffset: 0, 
      showDown: 0,
      status:1,
      productItems: [
        // {
        //   "navigatorUrl": "/page/category/details",
        //   "imgUrl": "/images/index8.jpg",
        //   "title": "戴安娜玫瑰11枝红色康乃馨 11支，红色石竹梅4枝",
        //   "price": "338.00",
        //   "numPeople": "1517"
        // }
      ]
  },
  onLoad: function (options) {
    var that = this;
var text =options.text
var kclass = options.class
console.log(kclass) 
    wx.request({
      url: 'https://www.xiangmaihua.com/App/Category/sousuo', //仅为示例，并非真实的接口地址
      data: {
        text: text, kclass: kclass
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res.data)
        that.setData({
          productItems: res.data.d
        })
      }
    })

    
      wx.getSystemInfo({
          success: function (res) {
              that.setData({
                  isHide: 1,
                  sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth)/2 ,
                  sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
              });
          }
      });      
  },
  onReady() {
  },   
  
  tabClick: function (e) {  
      this.setData({
          sliderOffset: e.currentTarget.offsetLeft,
          activeIndex: e.currentTarget.id
      });
      if (e.currentTarget.id==0){
          this.setData({
              showDown: 1
          });
      }else{
        var that = this;
        var k = e.currentTarget.dataset.sas
        let arr = that.data.productItems;
        if (k=='num'){
          for (var i = 0; i < arr.length - 1; i++) {
            for (var j = 0; j < arr.length - 1 - i; j++) {
              if (arr[j][k] < arr[j + 1][k]) {
                var temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
              }
            }
          }
        }else{
            var that = this;
            var k = e.currentTarget.dataset.sas
            let arr = that.data.productItems;
            var t = that.data.status
            if (t == 0) {
              for (var i = 0; i <= arr.length - 1; i++) {
                for (var j = 0; j < arr.length - 1 - i; j++) {
                  if (arr[j][k] > arr[j + 1][k]) {
                    var temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                  }
                }
              }
              that.setData({
                productItems: arr,
                status: 1,
                showDown: 0
              })
            } else {
              for (var i = 0; i <= arr.length - 1; i++) {
                for (var j = 0; j < arr.length - 1 - i; j++) {
                  if (arr[j][k] < arr[j + 1][k]) {
                    var temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                  }
                }
              }
              that.setData({
                productItems: arr,
                status: 0,
                showDown: 0
              })
            }
        }
        that.setData({
          productItems: arr,
          showDown: 0
        })       
      }
      if (e.currentTarget.id == 3) {
        wx.redirectTo({
              url: "/page/category/flowers"
          })  
      }  
  },

  closeDropDown: function(e){//销量 人气 评价
    var that = this;
   var k= e.currentTarget.dataset.sas
    let arr = that.data.productItems;

    for (var i = 0; i < arr.length - 1; i++) {
      for (var j = 0; j < arr.length - 1 - i; j++) {
        if (arr[j][k] < arr[j + 1][k]) {
          var temp = arr[j];
          arr[j] = arr[j + 1];
          arr[j + 1] = temp;
        }
      }
    }
    that.setData({
      productItems: arr,
      showDown: 0
    })
  },
})